/**
 * 知识赛相关的接口替换成了新的接口，参数、返回结构均有变化，在这里统一处理，页面就可以保证最小改动了
 */

import axios from '@/libs/axios1'
import { responesAssemble } from '@/libs/utils'
let jsonHeaders = { 'content-type': 'application/json;charset=UTF-8' }

export const releaseKnowledgeGrade = data => {
	return axios({
		url: `/compete/contest/pushContestGrade`,
		method: 'put',
		data,
		headers: jsonHeaders
	})
}

// 将比赛状态转义，新接口返回的状态将多，统一为原接口的三个状态
const statusEscape = status => {
	switch (status) {
		case 0:				// 准备中
		case 1: return '0'	// 准备完毕
		case 2:				// 开始中
		case 3:				// 裁判评判阶段
		case 5: return '1'	// 暂停
		case 4: return '2'	// 结束
		default: return '0'
	}
}

// 获取竞赛列表（知识赛）
export const compitionlist = obj => {
	let params = {
		pageIndex: obj.page || 1,
		pageSize: obj.rows || 10,
		order: obj.order,
		sortBy: obj.sortBy,
		id: obj.id,
		name : obj.contestName,
		projectId: obj.prjId,
		iconPath: obj.iconPath,
		type: 3,
		startTime: obj.startDate,
		endTime: obj.endDate,
		createTime: obj.createTime,
		creator: obj.createUserId,
		description: obj.description,
		companyId: obj.companyId
	}
	return new Promise(resolve => {
		axios({
			url: `/compete/contest/page`,
			method: 'get',
			params: params,
			headers: jsonHeaders
		}).then(res => {
			let { code, data, message } = res
			let total = data.total || 0
			let items = (data.items || []).map(item => {
				return {
					...item,
					"createUserId": item.creator,
					"endDate": item.endTime,
					"companyName": item.companyName,
					"description": item.description,
					"contestName": item.name,
					"companyId": item.companyId,
					"createTime": item.createTime,
					"activityStatus": statusEscape(item.progress),
					"createUser": item.creatorName,
					"id": item.id,
					"iconPath": item.iconPath,
					"startDate": item.startTime,
					"matchMode": item.type,
					prjId: item.projectId,
					extension: item.extension
				}
			})
			resolve({
				code: code,
				message: message,
				attribute: {
					totalPage: Math.ceil(total / params.pageSize),
					dataList: items,
					totalCount: total,
					currentPage: params.pageIndex
				}
			})
		})
	});
}

//添加竞赛（知识赛）
export const addcompetition = obj => {
	let params = {
		companyId: obj.companyId,
		createTime: obj.createTime,
		creator: obj.createUserId,
		description: obj.description,
		endTime: obj.endDate,
		iconPath: obj.iconPath,
		id: obj.id,
		name: obj.contestName,
		progress: 0,
		startTime: obj.startDate,
		type: 3,
		extension: obj.extension
	}
	return axios({
		url: `/compete/contest`,
		method: 'post',
		data: params,
		headers: jsonHeaders
	})
}
//删除竞赛（知识赛）
export const delcompetition = params => {
	return axios({
		url: `/compete/contest/${params}`,
		method: 'delete',
		headers: jsonHeaders
	})
}
//修改竞赛场景（知识赛）
export const modifycompetition = obj => {
	// obj内部还包了一层
	let id = obj.id
	obj = obj.obj
	let params = {
		companyId: obj.companyId,
		createTime: obj.createTime,
		creator: obj.createUserId,
		description: obj.description,
		endTime: obj.endDate,
		iconPath: obj.iconPath,
		id: id,
		name: obj.contestName,
		progress: 0,
		startTime: obj.startDate,
		type: 3,
		extension: obj.extension
	}
	return axios({
		url: `/compete/contest/${params.id}`,
		method: 'put',
		data: params,
		headers: jsonHeaders
	})
}
//修改竞赛状态
export const changeCompetitionSta = (id,params) => {
	let status = (params.state === 'start') ? '2' : '4'
	return axios({
		url: `/compete/contest/updateState/${id}?state=${status}`,
		method: 'put',
		headers: jsonHeaders
	})
}
//获取竞赛详情
export const getCompetitionDetail = (id) => {
	return new Promise(resolve => {
		return axios({
			url: `/compete/contest/${id}`,
			method: 'get',
			headers: jsonHeaders
		}).then(res => {
			let { code, message } = res
			let data = res.data || {}
			let item = {
				"createUserId": data.creator,
				"endDate": data.endTime,
				"companyName": data.companyName,
				"description": data.description,
				"contestName": data.name,
				"companyId": data.companyId,
				"createTime": data.createTime,
				"activityStatus": statusEscape(data.progress),
				"createUser": data.creatorName,
				"id": data.id,
				"iconPath": data.iconPath,
				"startDate": data.startTime,
				"matchMode": data.type,
				prjId: data.projectId,
				extension: data.extension
			}
			resolve({
				code: code,
				message: message,
				attribute: {
					data: item
				}
			})
		})
	});
}

// 获取流量配置信息
export const ctfFlowConfig = params => {
	return axios({
	    url: `/compete/ctfFlow/page`,
	    method: 'get',
	    params: params,
	    headers: jsonHeaders
	})
}


//删除当前比赛所有流量配置信息(drill.vue)
export const newDelAllCtfFlowConfig = sceneId => {
	return axios({
		url: `/compete/ctfFlow/delFlowData/${sceneId}`,
		method: 'delete',
		headers: jsonHeaders
	})
}

//新自动导入流量配置(ctfCompeteDetail.vue)
export const newSaveFlowDataByPrj = (sceneId) => {
  return axios({
    url: `/compete/ctfFlow/saveFlowData/${sceneId}`,
    method: 'post',
    headers: jsonHeaders
  })
}
//设置流量配置redis缓存(ctfCompeteDetail.vue)
export const setCtfFlowRedis = (params) => {
  return axios({
    url: `/compete/ctfFlow/setCtfFlowRedis`,
    method: 'post',
    headers: jsonHeaders,
    params:params
  })
}

//清空工程(竞赛演练详情)
export const clearProject = (projectId) => {
	return axios({
		url: `/topo-edit/project/clear/${projectId}`,
		method: 'post',
		headers: jsonHeaders
	})
}

//导出报告(知识赛)
export const exportReport= sceneId => {
	return new Promise(resolve => {
		axios({
			url: `/compete/integral/export/${sceneId}`,
			method: 'get',
			headers: jsonHeaders
		}).then(res => {
			let ret = responesAssemble(res)
			ret.attribute.data = ret.attribute
			resolve(ret)
		})
	});
}

// 以PDF的格式导出积分排名
export const exportReport4PDF= (sceneId, sceneName ) => {
	return axios({
		url: `/compete/integral/pdf/${sceneId}/${sceneName}`,
		method: 'get',
		responseType:'blob',
		headers: jsonHeaders
	});
}
